package com.ygo.feihua;
import android.animation.*;
import android.content.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.view.View.*;
import android.view.animation.*;
import android.widget.*;
import android.widget.AdapterView.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.adapter.*;
import com.ygo.feihua.util.*;
import java.util.*;

import android.support.v7.widget.Toolbar;
import android.view.View.OnClickListener;
import com.ygo.feihua.view.*;
import cn.bmob.v3.datatype.*;
import android.util.*;
import android.net.*;

public class SqPinglun extends AppCompatActivity implements OnClickListener
{
	LayoutInflater ll;
	
	String id;
	boolean fsing=true;
	Gongju gj;
	
	private ListView listView=null;
	private Button pl_fs;
	private EditText pl_ed;
	private ImageView pl_touxiang;
	private TextView pl_name,pl_sj,pl_hf,pl_qx;
	private ProgressBar pl_jz;
	private ShequMessageLayout sq_message;
	ImageView pl_image;
	
	private UserManagement um;
	
	LinearLayout pl_hfl;
	Tiezi currentTie;
	PingLun plh;
	PlAdapter plz;
	PinglunManagement sqm;
	List<PingLun> data;
	
	DialogUtils du;

	private Toolbar tool;
	private MyUser tieZuo;
	private MyUser hfUser;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shequ_pinglun);
		
		SdkInitManagement.getDx().initBmobSdk();
		
		gj = Gongju.getdx(this);
		listView = (ListView)findViewById(R.id.list);  
	 	pl_ed = (EditText) findViewById(R.id.pl_ed);
		pl_fs = (Button) findViewById(R.id.pl_fs);
		pl_hf=(TextView) findViewById(R.id.pl_hf);
		pl_qx=(TextView) findViewById(R.id.pl_qx);
		pl_hfl=(LinearLayout) findViewById(R.id.pl_hfl);
		pl_jz=(ProgressBar) findViewById(R.id.pl_jz);
		tool=(Toolbar) findViewById(R.id.toolbar);
		pl_image=(ImageView) findViewById(R.id.pl_image);
		um=UserManagement.getDx();
		du=DialogUtils.getdx(this);
		sqm=PinglunManagement.getdx();
		data= sqm.getData();
		gj.cshToolbar(tool,"帖子详情");
		ll = ll.from(SqPinglun.this);
		
		pl_image.startAnimation(AnimationUtils.loadAnimation(this,R.anim.shake_y));
		
		final View v=ll.inflate(R.layout.pl_zitem, null);
		pl_touxiang = (ImageView) v.findViewById(R.id.pl_touxiang);
		pl_name = (TextView) v.findViewById(R.id.sq_name);
		pl_sj = (TextView) v.findViewById(R.id.sq_time);
		sq_message=(ShequMessageLayout) v.findViewById(R.id.sq_message);
		tool.setOnMenuItemClickListener(onMenuItemClick);
		
		tool.setOnClickListener(this);
		pl_hfl.setVisibility(View.GONE);
		pl_qx.setOnClickListener(this);
		pl_fs.setOnClickListener(this);
		listView.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					if(p3!=0){
						gj.ShowKeyboard(pl_ed);
						plh=(PingLun) plz.getItem(p3-1);
						hfUser=((PingLun) plz.getItem(p3-1)).getUser();
						pl_hf.setText("回复:"+plh.getUser().getUsername());
						pl_hfl.setVisibility(View.VISIBLE);
					}
					// TODO: Implement this method
				}
			});
			
		listView.setOnItemLongClickListener(new OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					if(p3!=0){
					gj.fzMessage(data.get(p3-1).getneirong());
					gj.show("已评论复制到剪贴板");
					}
					// TODO: Implement this method
					return true;
				}
			});
			
			
		
		Intent intent=this.getIntent();
		Uri uri = intent.getData();

		if(uri!=null){

            // 链接所有数据
            String dataString = intent.getDataString();

			Log.e("SqPunglun","da  "+dataString);
			//Toast.makeText(A4.this,"dataString\n"+dataString,Toast.LENGTH_LONG).show();
            // 获取链接所携带的参数数据
			 String params = uri.getQueryParameter("params");
			Log.e("SqPinglun","params  "+params);
			id=params;
		}else{
			id = intent.getStringExtra("id");
		}
			
		BmobQuery<Tiezi> tz=new BmobQuery<Tiezi>();
		tz.include("tiezuo,tie_type");// 希望在查询帖子信息的同时也把发布人的信息查询出来
		tz.getObject(id, new QueryListener<Tiezi>(){

				@Override
				public void done(final Tiezi p11, BmobException p2)
				{
					if(p2==null){
						currentTie=p11;					
						tieZuo=p11.gettiezuo();
						hfUser=tieZuo;
						currentTie.increment("viewed");
						currentTie.update(new UpdateListener(){

								@Override
								public void done(BmobException p1)
								{
									// TODO: Implement this method
								}									
							});
						BmobQuery<PingLun> pl=new BmobQuery<PingLun>();
						pl.addWhereEqualTo("tiezi", p11);		
						pl.include("user,hfneirong");// 希望在查询帖子信息的同时也把发布人的信息查询出来
						pl.order("createdAt");
						pl.setLimit(200);
						pl.findObjects( new FindListener<PingLun>(){

								@Override
								public void done(List<PingLun> p1, BmobException p2)
								{
									if(p2==null){
										data.clear();
										for (PingLun pp:p1)
										{  
											data.add(pp);
										}  
										pl_touxiang.setOnClickListener(new OnClickListener(){

												@Override
												public void onClick(View p1)
												{
													Intent intent=new Intent(SqPinglun.this,YonghuMingpian.class);
													intent.putExtra("id",p11.gettiezuo().getObjectId());
													startActivity(intent);
													// TODO: Implement this method
												}
											});
										listView.addHeaderView(v);
										plz = new PlAdapter(SqPinglun.this,data,listView);
										listView.setAdapter(plz); 
										
												
										pl_name.setText(p11.gettiezuo().getUsername());
										pl_sj.setText(p11.getCreatedAt());
										gj.setTouxiang(pl_touxiang,p11.gettiezuo());

										if(p11.getneirong().equals("")){
											sq_message.setMessage(p11.getMessage());
										}else{
											List<TieziMessage> ltm=new ArrayList<TieziMessage>();
											
											
											TieziMessage tm=new TieziMessage();
											tm.setImage(null);
											tm.setMessage(p11.getneirong());
											ltm.add(tm);
											if (p11.getImage() != null)
											{
												for (BmobFile bf:p11.getImage())
												{
													TieziMessage tm1=new TieziMessage();
													tm1.setImage(bf);
													tm1.setMessage("");
													ltm.add(tm1);
												}
											}
											sq_message.setMessage(ltm);
										}
										String fl="";
										if(p11.getTie_type()==null){
											fl="[默认分类]";
										}else{
											Log.e("名称","名称为"+p11.getTie_type());
											fl="["+p11.getTie_type().getName()+"]";
										}
										tool.setTitle(fl+p11.gettitle());
										pl_image.clearAnimation();
										pl_image.setVisibility(View.GONE);
										plz.notifyDataSetChanged();
									}
									// TODO: Implement this method
								}
							});
					}else{
						pl_image.clearAnimation();
						pl_image.setVisibility(View.GONE);
						gj.toastBmobException(listView,"加载失败",p2);
					}
					// TODO: Implement this method
				}
			});
	}

	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.toolbar:
				gj.fzMessage(currentTie.gettitle());
				gj.show("已复制帖子标题到剪贴板");
				break;
			case R.id.pl_qx:
				plh=null;
				hfUser=currentTie.gettiezuo();
				pl_hfl.setVisibility(View.GONE);
				break;
			case R.id.pl_fs:
				sendMessage();
				break;
		}
		
		// TODO: Implement this method
	}

	private void sendMessage(){
		if(fsing){
			if (um.getUser() == null)
			{
				gj.show("还没登录喵~");
				um.logInUser(SqPinglun.this);
				//startActivity(new Intent(SqPinglun.this, YonghuZhuce.class));
			}else{
				if (plz==null)
				{
					gj.show("请等待信息加载完毕后再评论");
				}else{
					final String s=pl_ed.getText().toString().trim();
					if (s.equals(""))
					{
						gj.show("发送信息不能为空");
					}else{
						if(!gj.isfs(s)){
							gj.show("发送内容包含敏感词");
						}else{
							pl_fs.setVisibility(View.GONE);
							pl_jz.setVisibility(View.VISIBLE);
							//防止重复发送
							fsing=false;
							final PingLun pl=new PingLun();
							pl.setUser(um.getUser());
							pl.setneirong(s);
							pl.setTiezi(currentTie);
							pl.setHfneirong(plh);
							pl.save(new SaveListener<String>(){

									@Override
									public void done(String p1, BmobException p2)
									{
										if(p2==null){
											if(hfUser!=null&&!hfUser.equals(um.getUser())){

												MessageAlert me1=new MessageAlert();
												me1.setSenduser(um.getUser());
												me1.setSendmessage(s);
												me1.setHftie(currentTie);
												me1.setHflx(1);
												me1.setHfhf(plh);
												me1.setIsShow(false);
												if(plh!=null){
													me1.setHfuser(plh.getUser());
												}else{
													me1.setHfuser(currentTie.gettiezuo());
												}
												me1.save(new SaveListener<String>(){

														@Override
														public void done(String p1, BmobException p2)
														{

															// TODO: Implement this method
														}
													});

											}


											pl_ed.setText("");
											currentTie.increment("pls");
											currentTie.update(new UpdateListener(){

													@Override
													public void done(BmobException p1)
													{
														// TODO: Implement this method
													}									
												});

											plh=null;
											pl_hfl.setVisibility(View.GONE);
											pl.setObjectId(p1);
											plz.add(pl);
											fsing=true;
											gj.show("评论成功");
											listView.setSelection(data.size()-1);
										}else{
											gj.toastBmobException(pl_fs,"评论失败",p2);

											//	gj.show("评论失败" + p2);
											fsing=true;

										}

										pl_fs.setVisibility(View.VISIBLE);
										pl_jz.setVisibility(View.GONE);
										// TODO: Implement this method
									}						
								});}
					}
				}
			}
		}
	}
	
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
				case R.id.tz_fx:
					String url="http://ourygo.top/html/detail.html?id="+currentTie.getObjectId();
					Intent textIntent = new Intent(Intent.ACTION_SEND);
					textIntent.setType("text/plain");
					textIntent.putExtra(Intent.EXTRA_TEXT, url);
					startActivity(Intent.createChooser(textIntent, "帖子分享"));
					break;
			}
			// TODO: Implement this method
			return true;
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method

		getMenuInflater().inflate(R.menu.menu_pinglun, menu);
		return super.onCreateOptionsMenu(menu);
	}

	
	
}
